package package_20220905;

import java.util.*;

public class Array {
	public static void main(String[] args) {
		long[] arr = new long[8];
		int nItem = 0;
		//1. insert item
		long data = 567;
		arr[nItem++] = data;
		data = 789;
		arr[nItem++] = data;
		//...
		data = 123;
		arr[nItem++] = data;
		data = 555;
		arr[nItem++] = data;
		
		// 遍历 arr
		for(int i =0; i < nItem; i++){
			System.out.println(arr[i]);
		}
		
		// 2. find 123
		long key = 123;
		
		for(int j = 0;j<4;j++){
			if(arr[j] == key){
				// output index of key
				String msg = String.format("Found %d in %d", key, j);
				System.out.println(msg);
			}
		}
		
		// 3. delete
		boolean flag = true;
		int j = 0;
		while(flag) {
			//3.1 设置需要删除的元素key
			System.out.print("请输入要删除元素:");
			Scanner scanner = new Scanner(System.in);
			key = scanner.nextInt();
			//3.2 先在数组中找到key这个元素, 如果在数组中找不到这个元素则重新输入
			for(j = 0; j < 4; j++){
				if(arr[j] == key){
					// output index of key
					String msg = String.format("Found %d in %d", key, j);
					System.out.println(msg);
					flag = false;
					break;
				}
			}
			System.out.println("数组中不存在这个元素, 请重新输入!");
		}
		
		//3.3 删除这个元素
		for(int x = j; x < nItem-1; x++){
			arr[x] = arr[x+1];
		}
		nItem--;
		
		// 3.3 输出删除key后的arr数组
		System.out.println("After delete 789");
		for(int i =0; i < nItem; i++){
			System.out.println(arr[i]);
		}		
	}
}

